using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

namespace AspNetThemesExtensions
{
    /// <summary>
    /// Wraps logic for files
    /// </summary>
    public class ThemeVirtualFile : System.Web.Hosting.VirtualFile
    {
        private string _virtualPath;
        private string _baseVirtualThemePath;

        public ThemeVirtualFile(string virtualPath, string baseVirtualThemePath)
            : base(virtualPath)
        {
            _virtualPath = virtualPath;
            _baseVirtualThemePath = baseVirtualThemePath;
        }

        public override Stream Open()
        {
            if (!File.Exists(HttpContext.Current.Server.MapPath("~" + _virtualPath)))
                return File.OpenRead(HttpContext.Current.Server.MapPath("~" + _baseVirtualThemePath));
            
            return File.OpenRead(HttpContext.Current.Server.MapPath(_virtualPath));
        }
    }
}
